 
// 更新数据界面 2017-5-31 11:40

#import "HDowloadDataViewController.h"
#import "HDownloadDataTableViewCell.h"
#import "HDownloadExamViewController.h"
@interface HDowloadDataViewController ()

@property NSMutableArray* downloadWhich;
@property UILabel *myTableViewHeaderTitle;
@property HttpDownload* httpDownload;
@property NSTimer* myTimer;
@property (nonatomic ,assign)int timeout;
@end

@implementation HDowloadDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *downloadBtn = [[UIBarButtonItem alloc] initWithTitle:@"下载" style:UIBarButtonItemStylePlain target:self action:@selector(didDownloadButttonClicked:)];
    
    self.navigationItem.rightBarButtonItem = downloadBtn;
    
    self.myTableView.frame = CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight);
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.tableFooterView = [[UIView alloc]init];
    
    
    self.myTableViewHeaderTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - 16, 50)];
    self.myTableViewHeaderTitle.text = @"共选择了0项";
    self.myTableViewHeaderTitle.textAlignment = NSTextAlignmentRight;
    self.myTableViewHeaderTitle.textColor = UIColor.brownColor;
    self.myTableViewHeaderTitle.backgroundColor = RGB(212, 212, 212);
    [self.myTableView setTableHeaderView:self.myTableViewHeaderTitle];

    [self.view addSubview:self.myTableView];
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(instancetype)init{

    self.DataSource = HttpDownload.theDownloadItem;
    self.downloadWhich = [[NSMutableArray alloc]init];
    for (int i=0; i<self.DataSource.count; i++) {
        [self.downloadWhich addObject:@"0"];
    }
    
    self.myTableView = [[UITableView alloc]init];
    
    
    return self;
}

- (IBAction)didDownloadButttonClicked:(id)sender {
    [HCommon popupMessage:@"正在下载，请稍候"];
    [self download];
}

- (IBAction)doloadSwitchChanged:(id)sender{
   
    UISwitch *switchControl = sender;
    
    self.downloadWhich[switchControl.tag] = switchControl.on ? @"1" : @"0";
    
    [self countDownloadItem];
}

-(void) countDownloadItem{

    NSInteger count = 0;
    for (NSString *val in self.downloadWhich) {
        if([val  isEqual: @"1"]){
            count++;
        }
    }
 
    UILabel *title = self.myTableViewHeaderTitle;
    title.text = [NSString stringWithFormat:@"共选择了%ld项",(long)count];
    
    [self.myTableView setTableHeaderView:title];
    
}

-(void) download{
    
    self.httpDownload = [[HttpDownload alloc]initWithDownloadWhich:self.downloadWhich];
    [self.httpDownload startDownload];
    // 这里使用了异步多线程下载 所以 需要不知道什么时候下载结束
    self.timeout = 60 * 3;
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled:) userInfo:@1 repeats:YES];
}

- (void)downloadQuality
{
    self.httpDownload = [[HttpDownload alloc]initWithDownloadWhich:self.downloadWhich];
    [self.httpDownload startDownLoadQuality];
    // 这里使用了异步多线程下载 所以 需要不知道什么时候下载结束
    self.timeout = 60 * 3;
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled:) userInfo:@1 repeats:YES];
}

-(void)downLoadExam {
   
        self.httpDownload = [[HttpDownload alloc]initWithDownloadWhich:self.downloadWhich];
        [self.httpDownload startDownExamLoad];
        // 这里使用了异步多线程下载 所以 需要不知道什么时候下载结束
        self.timeout = 60 * 3;
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled:) userInfo:@1 repeats:YES];
    
}
-(void)timerCalled:(NSTimer*) timer{
   self.timeout --;
    if(self.httpDownload.isDownloadOver == YES){
        [HCommon popupSuccessMessage:@"下载完成"];
        [self.myTimer invalidate];
        self.myTimer = nil;
    }
    if (self.timeout == 0) {
        [HCommon dismissAnimated:YES];
    }
}

#pragma mark UITableView 委托

//
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}


- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    //return @"P.S. 如果网络延迟较大，请一个一个项目下载";
    return nil;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.DataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cell";
    
    HDownloadDataTableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell = [[HDownloadDataTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    
    [cell.theItemSwitch setOn:NO animated:YES];
    [cell.theItemSwitch setTag:indexPath.row];
    
//    cell.accessoryView = cell.theItemSwitch;
    [cell.theItemSwitch addTarget:self action:@selector(doloadSwitchChanged:) forControlEvents:UIControlEventValueChanged];

    cell.theItemLabel.text = self.DataSource[indexPath.row];
    
    
    if (indexPath.row == 5) {
        cell.theItemSwitch.hidden = YES;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 5) {
        HDownloadExamViewController *HDownExam = [[HDownloadExamViewController alloc]init];
        HDownExam.title = self.DataSource[indexPath.row];
        [self.navigationController pushViewController:HDownExam animated:YES];
    }
}


@end
